package edu.hawaii.halealohacli.jcev.command;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.wattdepot.client.WattDepotClient;

/**
 * Tests the CurrentPower class.
 * 
 * @author Eldon Visitacion
 */
public class TestDailyEnergy {
  private String url = "http://server.wattdepot.org:8190/wattdepot/";
  private String tower = "Lokelani";
  private String day = " 2011-11-23";
  private WattDepotClient sampleClient = new WattDepotClient(url);
  private String timeout = "Connection timed out, trying again...";
  
  /**
   * tests if a connection is made.
   */
  @Test
  public void testConnection() {
    DailyEnergy de = new DailyEnergy();
    assertNotNull("Ensures the connection is made.", de); // change this
  }
  
  /**
   * Test that the arguments are correct or not.
   * @throws Exception if name doesn't exist or improper format of arguments
   */
  @Test
  public void testArugments() throws Exception {
    DailyEnergy de = new DailyEnergy();
    try {
      de.execute(tower + day, sampleClient);
    }
    catch (Exception e) {
      System.out.println(timeout);
      de.execute(tower + day, sampleClient);
    }
    assertTrue("True", de.testArgs());
    
    // only 1 argument
    try {
      de.execute("Lokelani", sampleClient);
    }
    catch (Exception e) {
      assertFalse("One argument given, should return false.", de.testArgs());
    }
    
    // more than two arguments
    try {
      de.execute("Lokelani 2011-11-11 awesome", sampleClient);
    }
    catch (Exception e) {
      assertFalse("More than two arguments given, should return false.", de.testArgs());
    }
    
    // incorrect date format
    try {
      de.execute("Lokelani 2011-11-dd", sampleClient);
    }
    catch (Exception e) {
      assertFalse("Incorrect date format. Should be yyyy-mm-dd", de.testArgs());
    }
    
    // argument with wrong name
    try {
      de.execute("LokeDizzle 2011-11-11", sampleClient);
    }
    catch (Exception e) {
      assertFalse("Name doesn't exist, returns false", de.testArgs());
    }
    
    // No arguments
    try {
      de.execute("", sampleClient);
    }
    catch (Exception e) {
      assertFalse("No arguments, returns false", de.testArgs());
    }
  }
  
  /**
   * Verifies that the time covers the entire day's worth.
   * @throws Exception If an exception was thrown by the command (i.e. bad arguments) or any of its
   * function calls.
   */
  @Test
  public void testDay() throws Exception {
    DailyEnergy de = new DailyEnergy();
    try {
      de.execute(tower + day, sampleClient);
    }
    catch (Exception e) {
      System.out.println(timeout);
      de.execute(tower + day, sampleClient);
    }
    de.setAll(sampleClient, tower, de.getCalendar());
    assertEquals("Hours should be 23", 23, de.getEndTime().getHour() - de.getStartTime().getHour());
    assertEquals("Hours should be 59", 59, 
        de.getEndTime().getMinute() - de.getStartTime().getMinute());
    assertEquals("Hours should be 59", 59, 
        de.getEndTime().getSecond() - de.getStartTime().getSecond());
  }
  
  /**
   * ensures we're getting the right tower.
   * @throws Exception If method call throws exception.
   */
  @Test
  public void testCorrectName() throws Exception {
    DailyEnergy de = new DailyEnergy();
    try {
      de.execute(tower + day, sampleClient);
    }
    catch (Exception e) {
      System.out.println(timeout);
      de.execute(tower + day, sampleClient);
    }
    de.setAll(sampleClient, tower, de.getCalendar());
    System.out.println(de.getName() + " " + tower);
    assertEquals("Ensures we are getting the correct tower.", de.getName(), tower);
  }
  
  /**
   * Ensures we're getting an energy reading.
   * @throws Exception If method call throws exception.
   */
  @Test
  public void testEnergy() throws Exception {
    DailyEnergy de = new DailyEnergy();
    try {
      de.execute(tower + day, sampleClient);
    }
    catch (Exception e) {
      System.out.println(timeout);
      de.execute(tower + day, sampleClient);
    }
    de.setAll(sampleClient, tower, de.getCalendar());
    System.out.println("Kilowatts used: " + de.getKiloWattsUsed());
    assertTrue("Ensures we are getting an energy reaing.", de.getKiloWattsUsed() > 0);
  }
  
}